package com.djl.sqtg.controller;

import com.alibaba.fastjson.JSONObject;
import com.djl.sqtg.entity.Carousel;
import com.djl.sqtg.service.ICarouselService;
import com.djl.sqtg.util.JsonResult;
import com.djl.sqtg.util.TencentCOSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("carousel")
public class CarouselController extends BaseController {
    @Autowired
    private ICarouselService carouselService;

    /*
    * 获取轮播图图片数据
    * @return 多个轮播图图片数据
    * */
    @GetMapping({ "getAllCarouselImg"})
    public JsonResult<List<Carousel>> getAllCarouselImg(){
        System.out.println("轮播图请求进入-------------------");
        List<Carousel> data=carouselService.getAllCarouselImg();
        System.out.println("轮播图请求结束-------------------");
        return new JsonResult<List<Carousel>>(OK,data);
    }

    @PostMapping("updateCarouselImg")
    public JSONObject updateCarouselImg(@RequestParam("file") MultipartFile file, @RequestParam("cid")  Integer cid) {
        System.out.println("进入更换轮播图控制层");
        System.out.println("cid------>"+cid);
        Carousel carousel=new Carousel();
        carousel.setCid(cid);
        carousel.setImg(TencentCOSUtil.UploadIMG(file,"carousel"));
        System.out.println(carousel.getImg());
        carouselService.updateCarouselImg(carousel);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "修改成功");
        return jsonObject;
    }

}
